<?php
ob_start();
include("dbinfo.inc.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database"); 

$query="select ID from TrailHead where name like '$_POST[trailHeadName]';";
$resultID=mysql_query($query);
if (!$resultID) {
  echo "Error selecting ID from TrailHead: " . mysql_error();
  die("");
}
$headID=mysql_result($result,0, "ID");

$query="select ID from Trail where name like '$_POST[name]';";
$resultID=mysql_query($query);
if (!$resultID) {
  echo "Error selecting ID from Trail: " . mysql_error();
  die("");
}
$trailID=mysql_result($result,0, "ID");

$query="
DELETE FROM Trail WHERE name like '$_POST[name]';
";
if (!mysql_query($query)) {
  echo "Error deleting entry from Trail: " . mysql_error();
}


$query = "
INSERT INTO Trail (ID, headID, name, length, highest_altitude, difficulty, notes)
VALUES (
	$trailID,
	$headID,
	'$_POST[name]', 
	$_POST[length],
	$_POST[highest_altitude],
	$_POST[difficulty],
	'$_POST[notes]'
	)";

if (!mysql_query($query)) {
  echo "Error inserting entry into Trail: " . mysql_error();
}

mysql_close();
header("Location: index.php");
ob_flush();
?> 
